package com.forum.service.impl;

import com.forum.mapper.AnnouncementMapper;
import com.forum.mapper.PostMapper;
import com.forum.mapper.UserStatsMapper;
import com.forum.model.entity.Post;
import com.forum.model.vo.AnnouncementVO;
import com.forum.model.vo.ActiveUserVO;
import com.forum.model.vo.PostVO;
import com.forum.service.HomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class HomeServiceImpl implements HomeService {

    private final PostMapper postMapper;
    private final AnnouncementMapper announcementMapper;
    private final UserStatsMapper userStatsMapper;

    @Override
    public List<PostVO> getHotPosts() {
        List<Post> posts = postMapper.findHotPosts();
        return posts.stream()
                .map(this::convertToPostVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<PostVO> getLatestPosts() {
        List<Post> posts = postMapper.findLatestPosts();
        return posts.stream()
                .map(this::convertToPostVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<AnnouncementVO> getAnnouncements() {
        return announcementMapper.findActiveAnnouncements().stream()
                .map(announcement -> {
                    AnnouncementVO vo = new AnnouncementVO();
                    BeanUtils.copyProperties(announcement, vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<ActiveUserVO> getActiveUsers() {
        return userStatsMapper.findActiveUsers();
    }

    private PostVO convertToPostVO(Post post) {
        PostVO vo = new PostVO();
        BeanUtils.copyProperties(post, vo);
        // 设置作者信息
        vo.setAuthorName(post.getAuthorName());
        vo.setAuthorAvatar(post.getAuthorAvatar());
        return vo;
    }
} 